<?php

namespace Common\Model;
use Think\Model;

/**
 * Class BaseModel
 * @package Common\Model
 * 基础模型
 */
class BaseModel extends Model{

    public $pix = "";
    public $fieldsStr = '*';
    public $orderStr = '';

    public function getAllPage($Page, $map=array()){
        //区分是否开启缓存，后台的话就不开启，因为实时性较高，前台就开启
        $list = $this->field($this->fieldsStr)->where($map)
            ->order($this->orderStr)->limit($Page->firstRow.','.$Page->listRows)->select();
        return $list;
    }

    /**
     * @param array $map where查询数组
     * @param $cur_page 当前页
     * @param $rows_num 每页的条数
     * @return mixed
     */
    public function get_list_use_page( $map=array(), $cur_page, $rows_num){
        //区分是否开启缓存，后台的话就不开启，因为实时性较高，前台就开启
        $count = $this->where($map)->count();
        $list = $this->where($map)->order($this->orderStr)->page($cur_page.",$rows_num")->select();
        return array('total'=>$count, 'list'=>$list);
    }

    public function get_field_array($first_key, $field=false){
        $array = $this->field_array();
        return get_field_array($array, $first_key, $field);
    }

    /**
     * @return mixed获取全部
     */
    public function getAll($limit=0){
        if($limit == 0){
            $list = $this->field($this->fieldsStr)->order($this->orderStr)->select();
        }else{
            $list = $this->field($this->fieldsStr)->order($this->orderStr)->limit($limit)->select();
        }
        return $list;
    }

    /**
     * 根据ID查询
     * @param int $id
     * @return mixed
     */
    public function queryObj($value=0, $field){
        $map[$field] = array('eq', $value);
        $obj = $this->where($map)->find();
        return $obj;
    }

    /**
     * @param $field
     * @param $value
     * 根据字段查询
     */
    public function queryField($field, $value, $condition='eq', $limit=0){
        $map[$field] = array($condition, $value);
        if($limit == 0){
            $list = $this->field($this->fieldsStr)->where($map)->order($this->orderStr)->select();
        }else{
            $list = $this->field($this->fieldsStr)->where($map)->order($this->orderStr)->limit($limit)->select();
        }
        return $list;
    }

    public function checked_record($ids){
        $ids = array_filter(explode(',', $ids));
        foreach($ids as $id){
            $one = $this->queryObj($id, 'id');
            $one['ischecked'] = 1;
            $one['checkeduid'] = get_module_session('Admin', 'user_id');
            $this->save($one);
        }
    }

    /**
     * 传入一个$map
     * @param $ids
     */
    public function queryArray($map){
        return $list = $this->field($this->fieldsStr)->where($map)->order($this->orderStr)->select();
    }

    public function query_array_str($map, $str){
        return $list = $this->field($this->fieldsStr)->where($map)->where($str)->order($this->orderStr)->select();
    }

    /**
     * 增加
     * @param $post
     */
    public function addObj($post, $f_title='id'){
        //判重
        $data[$f_title] = array("eq", $post[$f_title]);
        $one = $this->where($data)->find();
        if(!$one){
            //如果没有记录，就添加
            foreach($post as $k=>$v){
                $data[$k] = $v;
            }
            $data['date'] = getNowTime();
            $data['createtime'] = getNowTime();
            return $this->add($data);
        }else{
            //反之更新
            foreach($post as $k=>$v){
                $one[$k] = $v;
            }
            return $this->save($one);
        }
    }

    /**
     * 使用传入的post数组进行数据操作，更新
     * @param $post
     */
    public function updateObj($post){
        foreach($post as $k=>$v){
            $data[$k] = $v;
        }
        return $this->save($data);
    }

    /**
     * 删除单个
     * @param $id
     */
    public function deleteObj($id){
        return $this->delete($id);
    }

    /**
     * 传入一个ID数组，批量删除
     * @param $ids
     */
    public function deleteArray($ids=array()){

        $idsStr = "";
        if(is_array($ids)){
            $idsStr = implode(',', $ids);
        }else{
            $idsStr = implode(',',array_filter(explode(',', $ids)));
        }
        return $this->delete($idsStr);
    }

}